<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form action="#" id="form">
      <label>
        用户名：
        <input type="text" name="user" id="user" />
      </label>
      <label>
        用户名：
        <input type="password" name="pass" id="pass" />
      </label>
      <button id="btn">登陆</button>
    </form>
    <script>
      const oForm = document.getElementById("form");
      const oUser = document.getElementById("user");
      const oPass = document.getElementById("pass");
      const oBtn = document.getElementById("btn");
      oForm.onsubmit = function () {
        myAjax("get", `/login?user=${oUser.value}&pass=${oPass.value}`).then(
          (val) => console.log(val)
        );
        return false;
      };

      function myAjax(method, url) {
        return new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.open(method, url);
          xhr.send();
          xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
              resolve(xhr.responseText);
            }
          };
          reject("失败");
        });
      }
    </script>
  </body>
</html>
